//
//  PhotoDetailViewController.swift
//  健身宝
//
//  Created by hzt on 2017/11/3.
//  Copyright © 2017年 hzt. All rights reserved.
//


import UIKit

class PhotoDetailViewController: UIViewController {
    var infoDict:NSDictionary!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        viewConfig()
    }
    
    func viewConfig(){
        view.backgroundColor = UIColor.white
        self.navigationController?.isNavigationBarHidden = true
        self.rdv_tabBarController?.isTabBarHidden = true

        //        let navBar = CustomNavBar(frame: CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.width, height: 64)))
        //        navBar.initialize(text: "详情")
        //        view.addSubview(navBar)
        
        self.navigationController?.isNavigationBarHidden = true
        self.rdv_tabBarController?.isTabBarHidden = true
        let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-64))
        //scrollView.contentInsetAdjustmentBehavior = .never
        scrollView.contentSize = CGSize(width: 0, height: 364+(UIScreen.main.bounds.height-69)/3)
        scrollView.backgroundColor = view.backgroundColor
        scrollView.showsVerticalScrollIndicator = false
        view.addSubview(scrollView)
        
        let imageView = UIImageView()
        let pURL = "http://172.24.10.175/workout/Uploads/"
        imageView.sd_setImage(with: URL(string: pURL+(infoDict.value(forKey: "thumb0") as! String)), placeholderImage: UIImage(named:"placeholder"))
        imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: (UIScreen.main.bounds.height-69)/3)
        scrollView.addSubview(imageView)
        

        let label = UILabel(frame: CGRect(x: 15, y: imageView.bounds.height+10, width: UIScreen.main.bounds.width-30, height: 20))
        label.text = "标题: \(infoDict.value(forKey: "title") as! String)"
        label.font = UIFont.systemFont(ofSize: 20)
        scrollView.addSubview(label)
        
        let telLabel = UILabel(frame: CGRect(x: 15, y: label.frame.origin.y+20, width: UIScreen.main.bounds.width-30, height: 20))
        telLabel.text = "发布时间: \(infoDict.value(forKey: "update_time") as! String)"
        telLabel.font = UIFont.systemFont(ofSize: 20)
        scrollView.addSubview(telLabel)
        

        
        let userLabel = UILabel(frame: CGRect(x: 15, y: telLabel.frame.origin.y+20, width: UIScreen.main.bounds.width-30, height: 20))
        userLabel.text = "发布人: \(infoDict.value(forKey: "userid") as! String)"
        userLabel.font = UIFont.systemFont(ofSize: 20)
        scrollView.addSubview(userLabel)
        
        let hitLabel = UILabel(frame: CGRect(x: 15, y: userLabel.frame.origin.y+20, width: UIScreen.main.bounds.width-30, height: 20))
        hitLabel.text = "点击次数: \(infoDict.value(forKey: "hits") as! String)"
        hitLabel.font = UIFont.systemFont(ofSize: 20)
        scrollView.addSubview(hitLabel)
        
        let sizeLabel = UILabel(frame: CGRect(x: 15, y: hitLabel.frame.origin.y+20, width: UIScreen.main.bounds.width-30, height: 20))
        sizeLabel.text = "图片大小: \(infoDict.value(forKey: "thumb0size") as? String)"
        sizeLabel.font = UIFont.systemFont(ofSize: 20)
        scrollView.addSubview(sizeLabel)
        
        let commentBtn = UIButton(frame: CGRect(x: 0, y: sizeLabel.frame.origin.y+20, width: 150, height: 44))
        commentBtn.backgroundColor = UIColor.red
        commentBtn.center.x = UIScreen.main.bounds.width/2
        commentBtn.setTitle("评论", for: .normal)
        commentBtn.layer.cornerRadius = 8
        commentBtn.adjustsImageWhenDisabled = false
        scrollView.addSubview(commentBtn)
        commentBtn.addTarget(self, action: #selector(comment), for: .touchUpInside)
        
        let backBtn = UIButton(frame: CGRect(x: 16, y: 0, width: 35, height: 30))
        backBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        backBtn.setTitle("返回", for: .normal)
        backBtn.setTitleColor(.black, for: .normal)
        scrollView.addSubview(backBtn)
        backBtn.addTarget(self, action: #selector(back), for: .touchUpInside)
    }
    
    @objc func comment(sender:UIButton){
        sender.isEnabled = false
        let dict = infoDict as! NSDictionary
                //print(dict)
                let commentArray = dict.value(forKey: "comment") as! NSArray
                let controller = SharingDetailController()
                controller.infoArray = commentArray.mutableCopy() as! NSMutableArray
                controller.userID = dict.value(forKey: "userid") as! String
                controller.shaituID = dict.value(forKey: "id") as! String
                //print(controller.userID)
                print(infoDict)
                //hidesBottomBarWhenPushed = true
                _ = navigationController?.pushViewController(controller, animated: true)
                //hidesBottomBarWhenPushed = false
    }
    
    @objc func back(){
        _ = navigationController?.popViewController(animated: true)
        self.navigationController?.isNavigationBarHidden = false
        self.rdv_tabBarController?.isTabBarHidden = false
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

